﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PatientsManagement
{
    class Validator
    {
ErrorProvider errorProvider;
public Validator(ErrorProvider errorProvider)
{
    this.errorProvider = errorProvider;
}

public void IsEmpty(Object control, string validationMessage)
{
    errorProvider.SetIconPadding(((TextBox)control), -18);
    String text = ((TextBox)control).Text;
    if (text == "")
        errorProvider.SetError((TextBox)control, validationMessage);
    else
        errorProvider.SetError((TextBox)control, "");
}

public void EmptyLengthValidation(Object control, string emptyMessage,string tooShortMessage, int lettersCount)
{
    errorProvider.SetIconPadding(((MaskedTextBox)control), -18);
    String text = ((MaskedTextBox)control).Text;
    String error = "";
    if (text == "")
        error += emptyMessage;

    if (text.Length < lettersCount)
    {
        if (error != "")
            error += Environment.NewLine;
        error += tooShortMessage;
    }
    errorProvider.SetError(((MaskedTextBox)control), error);
}

public void PasswordValidation(Object control, string emptyMessage, string tooShortMessage)
{
    errorProvider.SetIconPadding(((TextBox)control), -18);
    String text = ((TextBox)control).Text;
    String error = "";
    if (text == "")
        error += "Hasło nie może być puste";

    if (text.Length < 6)
    {
        if (error != "")
            error += Environment.NewLine;
        error += "Hasło musi zawierać co najmniej 6 znaków";
    }
    errorProvider.SetError(((TextBox)control), error);
}

public void PasswordValidation(Object control, string tooShortMessage)
{
    errorProvider.SetIconPadding(((TextBox)control), -18);
    String text = ((TextBox)control).Text;
    String error = "";
    if (text.Length < 6&&text != "")
    {
        error += "Hasło musi zawierać co najmniej 6 znaków";
    }
    errorProvider.SetError(((TextBox)control), error);
}

        public void PasswordValidation(Object control, List<string> messages)
        {
            errorProvider.SetIconPadding(((TextBox)control), -18);
            String error = "";
            for (int i = 0; i < messages.Count; i++)
            {
                if (i != messages.Count - 1)
                {
                    if (error != "")
                        error += Environment.NewLine;
                }
                error += messages[i];
            }
        }

        public bool NoValidation(List<TextBox> controls)
        {
            foreach (TextBox control in controls)
                if (errorProvider.GetError(control) != "")
                    return false;
            return true;
        }

        public bool NoValidation(List<MaskedTextBox> controls)
        {
            foreach (MaskedTextBox control in controls)
                if (errorProvider.GetError(control) != "")
                    return false;
            return true;
        }

        public bool ValidateUser(string userName)
        {
            List<string> users = Database.UsersData.GetUserNames();
            foreach (string user in users)
                if (userName.ToLower().Equals(user.ToLower()))
                    return true;
            return false;
        }

        public bool CheckPassword(string userName,string password)
        {
            return Database.UsersData.GetUserPassword(userName) == HelperMethods.HashPassword(password);
        }
    }
}
